package src.mian.java;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;
import java.io.*;

public class HDFSAppendExample {
    public static void main(String[] args) {
        String hdfsUri = "hdfs://192.168.254.100:8020";
        String filePath = "/test/world.txt";
        String localFilePath = "/D:/hadoop/temp.txt";
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsUri);
        try {
            FileSystem fs = FileSystem.get(conf);
            // Open the file for append
            FSDataOutputStream outputStream = fs.append(new Path(filePath));

            // Read the content of local file and append to HDFS file
            BufferedReader br = new BufferedReader(new FileReader(localFilePath));
            String line;
            while ((line = br.readLine()) != null) {
                outputStream.writeBytes(line + "\n");
            }
            // Close the streams
            br.close();
            outputStream.close();
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
